home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15238 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  73 lines

  1. Path: news.ultranet.com!usenet
  2. From: seedy@ultranet.com
  3. Newsgroups: comp.sys.lang.c++,comp.lang.c++
  4. Subject: Help needed in C++ Function templates
  5. Date: Thu, 04 Apr 96 17:29:36 PDT
  6. Organization: UltraNet Communications, Inc.
  7. Message-ID: <NEWTNews.828668110.2725.seedy@trigent.trigent.com>
  8. NNTP-Posting-Host: trigent.ultranet.com
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11. X-Newsreader: NEWTNews & Chameleon -- TCP/IP for MS Windows from NetManage
  12.  
  13.  
  14. When attempted to compile the following code using GNU's g++ (ver. 2.7.1)
  15. on HP-UX, I got the following error :
  16.  
  17. news.cc: In function `int equal(const class abc &, const class abc &)':
  18. news.cc:23: no match for `operator ==(class abc, class abc)'
  19.  
  20.  
  21. #include <iostream.h>
  22.  
  23. class abc {
  24.     public : 
  25.        abc( int x )
  26.       {
  27.       a = x;
  28.       b = 0;
  29.       }
  30.        int a;
  31.        int b;
  32.        inline int operator ==(abc x)
  33.            {
  34.               return ((a==x.a) && (b==x.b));
  35.            }
  36.     };
  37.  
  38.  
  39.  
  40. template < class Element>
  41. inline int equal (Element const& e1, Element const& e2) 
  42. {
  43.   return e1 == e2;
  44.  
  45. }
  46.  
  47. main()
  48. {
  49. abc a1(5), a2(5);
  50.  
  51. cout << "Return value is = " << equal(a1, a2) << endl;
  52. }
  53.  
  54.  
  55. Even though the 'operator ==' is overloaded in class abc, compiler is
  56. reporting match not found. By making the overloaded function 'operator =='
  57. as friend to 'class abc' I could resolve the problem, because in this case,
  58. this overloaded function becomes outsider function.
  59.  
  60. As far as I know, template instantiation for function 'equal will be done
  61. for 'class abc'(because the passed parameters are of type class abc), 
  62. and so the corresponding member function 'operator ==' can be used for 
  63. template variables e1 and e2.
  64.  
  65. Could any one explain why this error happens.  Please send 
  66. your response via mail to 'seedy@trigent.com' or post it here.
  67.  
  68. Cheers,
  69.  
  70. Praveen.
  71.  
  72.  
  73.